﻿namespace Bango.WindowsPhone7_5.SDK
{
	public class BangoProfile : IBangoPostable
	{
		#region Properties, variables

		public System.String ApplicationChannel { get; set; }
		public System.String ApplicationId { get; set; }
		public System.String ApplicationType { get; set; }
		public System.String ApplicationVersion { get; set; }
		public System.String ConnectionType { get; set; }
		public System.String CountryISO { get; set; }
		public System.String DeviceBrand { get; set; }
		public System.Boolean DeviceIsMobile { get; set; }
		public System.String DeviceMake { get; set; }
		public System.String DeviceModel { get; set; }
		public System.String DeviceVersion { get; set; }
		public System.Boolean IsRoaming { get; set; }
		public System.Double LocationAccuracy { get; set; }
		public System.String LocationCountryISO { get; set; }
		public System.String LocationRegion { get; set; }
		public System.String LocationSource { get; set; }
		public System.String LocationTown { get; set; }
		public System.Double LocationX { get; set; }
		public System.Double LocationY { get; set; }
		public System.Double LocationZ { get; set; }
		public System.String OperatingSystem { get; set; }
		public System.String OperatingSystemVersion { get; set; }
		public System.String OperatingSystemVersionName { get; set; }
		public System.String OperatorCode { get; set; }
		public System.String OperatorConnectionType { get; set; }
		public System.String OperatorName { get; set; }
		public System.Boolean Posted { get; set; }
		public System.Int32 SDKType { get; set; }
		public System.String SDKVersion { get; set; }
		public System.Int32 SessionRestartTime { get; set; }
		public System.String UserId { get; set; }

		public System.Int32 ProfileId { get; set; }

		public System.Collections.Generic.List<BangoEvent> Events { get; set; }

		#endregion

		public BangoProfile()
		{
			Events = new System.Collections.Generic.List<BangoEvent>();
		}

		internal BangoProfile(System.Int32 profileId, System.Int32 sdkType, System.String sdkVersion, System.String applicationId, System.String applicationType, System.String applicationChannel, System.String applicationVersion, System.String userId, System.String connectionType, System.Int32 sessionRestartTime, System.Boolean isRoaming, System.String locationSource, System.Double locationAltitude, System.Double locationLatitude, System.Double locationLongitude, System.Double locationAccuracy, System.String operatorCode, System.String operatorName, System.String operatorCountryIso, System.String brand, System.String device, System.String model, System.String product, System.String osVersion, System.String deviceVersion, System.String operatorConnectionType, System.String simOperator)
			: this()
		{
			ProfileId = profileId;
			SDKType = sdkType;
			SDKVersion = sdkVersion;
			ApplicationId = applicationId;
			ApplicationType = applicationType;
			ApplicationChannel = applicationChannel;
			ApplicationVersion = applicationVersion;
			UserId = userId;
			ConnectionType = connectionType;
			IsRoaming = isRoaming;
			LocationSource = locationSource;
			LocationY = locationLatitude;
			LocationX = locationLongitude;
			LocationAccuracy = locationAccuracy;
			SessionRestartTime = sessionRestartTime;
			OperatorCode = operatorCode;
			OperatorConnectionType = operatorConnectionType;
			OperatorName = operatorName;
			OperatingSystemVersion = osVersion;
			DeviceMake = device;
			DeviceModel = model;
			DeviceVersion = deviceVersion;

			if (double.IsNaN(locationAltitude))
			{
				LocationZ = 0;
			}
			else
			{
				LocationZ = locationAltitude;
			}

			//COMMENT : This is currently unavailable in WP7 - Code has been left in should it become available.
			// Theyve been set to defaults, so that no null values are saved to the db.
			DeviceBrand = brand;
			CountryISO = operatorCountryIso;
			LocationTown = "";
			OperatingSystemVersionName = "";
		}

		public BangoProfile GetPostProfile()
		{

			//System.Collections.Generic.List<BangoEventParameter> eventParameters;
			BangoProfile returnValue;

			returnValue = new BangoProfile(this.ProfileId, this.SDKType, this.SDKVersion, this.ApplicationId, this.ApplicationType, this.ApplicationChannel, this.ApplicationVersion, this.UserId, this.ConnectionType, this.SessionRestartTime, this.IsRoaming, this.LocationSource, this.LocationZ, this.LocationY, this.LocationX, this.LocationAccuracy, this.OperatorCode, this.OperatorName, System.String.Empty, System.String.Empty, this.DeviceMake, this.DeviceModel, System.String.Empty, this.OperatingSystemVersion, this.DeviceVersion, this.OperatorConnectionType, System.String.Empty);

			foreach (BangoEvent ev in this.Events)
			{
				if (!ev.Posted)
				{
					returnValue.Events.Add(ev);
				}
			}
			return returnValue;
		}

		public void UpdatePostedEvents()
		{
			foreach (BangoEvent bEvent in this.Events)
			{
				bEvent.Posted = true;
			}
		}

		internal void UpdateProfileConnectionDetails(bool isRoaming, string connType, string opConnType)
		{
			this.IsRoaming = isRoaming;
			this.ConnectionType = connType;
			this.OperatorConnectionType = opConnType;
		}

		internal void UpdateProfileLocation(System.Double locationLatitude, System.Double locationLongitude, System.Double locationAltitude, System.Double locationAccuracy)
		{
			this.LocationX = locationLongitude;
			this.LocationY = locationLatitude;
			this.LocationZ = locationAltitude;
			this.LocationAccuracy = locationAccuracy;
		}

		internal System.Int32 LogEvent(System.Boolean useOfflineStorage, System.String connectionType, System.String operatorConnectionType, System.Int32 errorLevel, System.String eventName, System.String eventDetail, System.String eventValue, System.String errorId, System.DateTime eventTime, System.DateTime eventLocalTime, System.Collections.Generic.List<BangoEventParameter> eventParameters)
		{
			int eventId = this.Events.Count + 1;
			try
			{
				this.Events.Add(new BangoEvent(this.ProfileId, eventId, connectionType, operatorConnectionType, errorLevel, eventName, eventDetail, eventValue, errorId, eventTime, eventLocalTime, eventParameters));
			}
			catch (System.Exception ex)
			{
				System.Console.WriteLine(ex.Message);
				return -1;
			}
			return eventId;
		}

		internal void SaveProfile(System.Boolean useOfflineStorage)
		{
			(new BangoStorage(useOfflineStorage)).SaveProfile(this.GetPostProfile());
			// events will be posted from memory        
			this.Events.Clear();
			this.Events.TrimExcess();
		}

	}
}
